<template>
  <div>
    <h3>{{msg}}</h3>
    <form action="">
      帐号：<input v-model="admin.username" type="text"/><br/>
      密码：<input v-model="admin.password" type="text"/><br/>
      <input type="button" value="登录" @click="login">
      <input type="button" value="重填">
    </form>
    <span style="color: red">{{errMsg}}</span>
  </div>
</template>

<script>
import axios from "axios";
import instance from "../utils/request";

export default {
  name: "login",
  data(){
    return {
      msg:"用户登录",
      admin:{},
      errMsg:""
    };
  },
  methods:{
    login(){ // 登录按钮的功能
      // 发异步请求，检查 帐号密码
      instance.post("/admin/login", this.admin)
        .then(res=>{
          let result = res.data
          if (result.login == "ok"){// 判断登录是否成功，成功
            // 往浏览器的本地存储中 存储信息 sessonid
            localStorage.setItem("token", result.token);
            this.$router.push({name:"welcome"}); // 到主页显示
          } else {// 不成功
            this.errMsg = result.errMsg
          }
        });
    }
  }
}
</script>

<style scoped>

</style>
